<?php
    use Swoole\Runtime;
    use Swoole\Coroutine;
    use function Swoole\Coroutine\run;

    Runtime::enableCoroutine();
    $s = microtime(true);

    echo "the task begin running...".PHP_EOL;
    run(function() {

        for ($j = 0; $j < 100; $j++) {
            $mysqli = new Mysqli('127.0.0.1', 'swoole4_ryloo_ic', '123456', 'swoole4_ryloo_ic');
            Coroutine::create(function () use ($j, $mysqli) {
                for ($i = 0; $i < 100; $i++) {
                    $sql = "INSERT INTO student (name, brands, age) VALUES (?, ?, ?)";
                    $query = $mysqli->prepare($sql);
                    $eq = $i*($j+1);
                    $val1 = '林小侠';
                    $val2 = '中大奖啦';
                    $val3 = '500';
                    $query->bind_param('sss', $val1, $val2, $val3);
                    if ($query->execute() == TRUE) {
                        echo "【ID:" . $eq . "】user entry saved successfully." . PHP_EOL;
                    } else {
                        echo "【ID:" . $eq . "】INSERT attempt failed" . PHP_EOL;
                    }
                }
            });
            //$mysqli->close();
        }

    });
    echo 'the task use ' . (microtime(true) - $s) . 'seconds. Congratulations on completing all the tasks!!!';
